﻿using AutoMapper;
using B.S.BaseData.Domain;
using B.S.BaseData.ErrorCode;
using B.S.BaseData.Infratructure.Interfares;
using B.S.BaseData.Write.Api.Application.Command;
using MediatR;

namespace B.S.BaseData.Write.Api.Application.CommandHander
{
    public class AddUserRoleCommandHander : IRequestHandler<AddUserRoleCommand, ApiResult<int>>
    {

        private readonly IMapper mapper;
        private readonly IBaseRequsitory<UserRole> baseRequsitory;

        public AddUserRoleCommandHander(IMapper mapper, IBaseRequsitory<UserRole> baseRequsitory)
        {
            this.mapper = mapper;
            this.baseRequsitory = baseRequsitory;
        }

        public async Task<ApiResult<int>> Handle(AddUserRoleCommand request, CancellationToken cancellationToken)
        {

            ApiResult<int> result = new ApiResult<int>();

            foreach (var item in request.RoleId)
            {
                var ur = new UserRole();
                ur.UserId = request.userRoles;
                ur.RoleId = item;
                await baseRequsitory.AddAsync(ur);
            }

            result.Code = ApiEnum.添加成功;
            result.Msg = ApiEnum.添加成功.ToString();
            return await Task.FromResult(result);
        }
    }
}
